#include "KeDialogToast.h"

#include <QPalette>
#include <QSpacerItem>
#include <QTimer>
#include <QDebug>



#ifdef Q_OS_WIN
#include "windows.h"
#include "windowsx.h"
#pragma comment (lib,"user32.lib")
#endif

namespace kepen {
namespace core {



KeDialogToast::KeDialogToast(QWidget *parent)
    :QDialog(parent)
{
    // QFont font;
    auto layout = new QHBoxLayout(this);//水平布局
    m_label = new QLabel(this);
    // 没有效果
    m_label->setStyleSheet("QLabel#specialLabel{color:#5e5e5e;background:#e8e8f6;border:1px solid #bbb;border-radius:6px;font-size: 15px;padding: 8px;font-weight: bold;}");
    // m_label->setStyleSheets("background-color:rgb(0,0,255);color:rgb(255,0,0);");
    m_label->setAlignment(Qt::AlignCenter); //文字居中
    m_label->setAttribute(Qt::WA_TransparentForMouseEvents);
    m_label->setObjectName("specialLabel");
    // font.setPointSize(15);
    // m_label->setFont(font);
    layout->addWidget(m_label);

    setLayout(layout);

    layout->addWidget(m_label);
    setWindowFlag(Qt::FramelessWindowHint); //隐藏菜单框
    this->setWindowOpacity(1.0);
    this->setAttribute(Qt::WA_TranslucentBackground);
    //构造函数中：setAttribute(Qt::WA_ShowWithoutActivating,true);
    //意思是显示小部件而不使其处于活动状态
    setAttribute(Qt::WA_ShowWithoutActivating, true);
    // setAttribute(Qt::WA_DeleteOnClose); // 设置关闭时析构


}

KeDialogToast::~KeDialogToast()
{

}

void KeDialogToast::show(const QString &text)
{
    //int r, int g, int b, int a = 255  QPalette::Window
    // QPalette p = palette();

    // p.setColor(QPalette::Window, QColor(0xcd, 0xcd, 0xcd, 0x60));
    // setPalette(p);

    m_label->setText(text);

    setWindowFlag(Qt::WindowStaysOnTopHint);//窗口置顶

    QDialog::show();

    QTimer::singleShot(4000, this, &KeDialogToast::onTimerClose);

#ifdef Q_OS_WIN
    HWND hwnd = (HWND)this->winId();
    SetCapture(hwnd);
#endif

}

void KeDialogToast::onTimerClose()
{
    this->deleteLater();
}

void KeDialogToast::mousePressEvent(QMouseEvent *event)
{
    // setAttribute(Qt::WA_NoMouseReplay);
    // QDialog::mousePressEvent(event);
    // this->accept();
    // qDebug()<<__FILE__<<__LINE__<<"mousePressEvent";

#ifdef Q_OS_WIN
    ReleaseCapture();

#endif
    setVisible(false);
    //没有用， 鼠标点击还是没有了
    QDialog::mousePressEvent(event);

}



}
}
